package cn.wlj.mq.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName TopicMqConfig
 * @Description TODO 主题模式（通配符配置类）
 * @Author WLJ
 * @Date 2024/1/21 13:37
 * @Version 1.0
 */
@Configuration
public class TopicMqConfig {
    // 队列名
    public static final String TOPIC_QUEUE = "topic_queue";
    public static final String TOPIC_QUEUES = "topic_queues";

    // 交换机名
    public static final String EXCHANGE_NAME = "boot_topic_exchange";

    // 创建队列
    @Bean
    public Queue topicQueue() {
        return new Queue(TOPIC_QUEUE);
    }
    @Bean
    public Queue topicQueues() {
        return new Queue(TOPIC_QUEUES);
    }

    // 声明topic类型的交换机
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    // 将队列绑定到交换机，并且指定路由键
    @Bean
    public Binding bindingExchangeTopic(Queue topicQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue).to(topicExchange).with("topic.message");
    }
    @Bean
    public Binding bindingExchangeTopics(Queue topicQueues, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueues).to(topicExchange).with("topic.#");
    }
}
